﻿//我们这边得到工厂的所有信息显示在页面
package com.webbei.Backstage.action;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.webbei.Backstage.service.BackstateSer;
import com.webbei.Backstagepojo.Industrycost;
import com.webbei.Backstagepojo.Labelcost;
import com.webbei.pojo.Ordercost;
import com.webbei.pojo.Orders;

public class SelectallindusByDateAndName extends ActionSupport{
	private BackstateSer bao;
    private List<Orders> orderlist;
    private List<Ordercost> ordercostlist;
    private List<Labelcost>list;
    private String indusname;
    private String orderid;
    private String ordername;
    private Ordercost cost;
    private Industrycost induscost;
    private List<Industrycost> induscostlist;
    private String time1;
    private String time2;
    private  Double alltotal;
    private String page;// 便于接受页面传过来的page
	private int pagenow =1;// 刚开始从第一页开始
	private int maxpage =20;// 每页显示最大记录条数
	private int pagecount;// 一共有几页
	private int yeshutotal;// 记录总条数
 public String execute(){

	     Double total=0.0;
	     Long orid = 0l;
	     int indusid=-1;
	    alltotal=0.0;
	    induscostlist=new ArrayList<Industrycost>();
		try {
			ordername=new String(ordername.getBytes("ISO-8859-1"),"UTF-8");
			indusname=new String(indusname.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		}

		  if(!orderid.equals("")&&orderid!=null)
			  orid=Long.parseLong(bao.chaifendingid(orderid));
	     	    else
	     	    	orid=-1l;

	    if(indusname!=null&&!indusname.equals("")){
	    	System.out.println("indusname:"+indusname);
           indusid=bao.getindusIdbyName(indusname);
	    }
	    else{
	         indusid=-1;
	    }


	    if(page!=null){
        	pagenow=Integer.parseInt(page);
        }
    	yeshutotal=bao.allOrdercostCount(orid, ordername, indusid, time1, time2);
	                                	pagecount = (yeshutotal% maxpage) == 0 ? (yeshutotal/maxpage): (yeshutotal/maxpage+ 1);
		  if (pagenow> pagecount) {
				pagenow=pagecount;
			}
			if (pagenow< 1) {
				pagenow = 1;
			}
		int start = (pagenow - 1) * maxpage;

		 orderlist=bao.allindusorderlist(orid,ordername,indusid,time1,time2,start);
		ordercostlist=bao.ordercost();
		for(int i=0;i<orderlist.size();i++){
			int k=0;
			induscost=new Industrycost();
			induscost.setOrdername(orderlist.get(i).getOrderName());
			induscost.setDatetime(orderlist.get(i).getOrderTime());
			induscost.setDingid(bao.get9dingid(orderlist.get(i).getOrderId()));

			induscost.setIndustryname(bao.getindusNamebyid(orderlist.get(i).getIndustryId()));
			for(int j=0;j<ordercostlist.size();j++){
				long a=orderlist.get(i).getOrderId();
				long b=ordercostlist.get(j).getOrderid();
				if(a==b){
					cost=bao.getoneordercost(a);
					if(cost.getFangdaosuocost()!=null)
					{ total+=cost.getFangdaosuocost();induscost.setFangdaosuocostname("防盗锁");induscost.setFangdaosuocost(cost.getFangdaosuocost());}
					if(cost.getFangzhengdiancost()!=null)
					{total+=cost.getFangzhengdiancost();induscost.setFangzhengdiancostname("防震垫配件");induscost.setFangzhengdiancost(cost.getFangzhengdiancost());}
					if(cost.getJiagongcost()!=null)

					{total+=cost.getJiagongcost();induscost.setJiagongcostname("加工成本");induscost.setJiagongcost(cost.getJiagongcost());}	


					if(cost.getJiguangcost()!=null)
					{total+=cost.getJiguangcost();induscost.setJiguangcostname("激光费用");induscost.setJiguangcost(cost.getJiguangcost());}
					if(cost.getKaoqicost()!=null)
					{total+=cost.getKaoqicost(); induscost.setKaoqicostname("烤漆费");induscost.setKaoqicost(cost.getKaoqicost());}
					if(cost.getLuosicost()!=null)
					{total+=cost.getLuosicost(); induscost.setLuosicostname("螺丝配件");induscost.setLuosicost(cost.getLuosicost());}
				    if(cost.getYinshuacost()!=null)
				    {total+=cost.getYinshuacost(); induscost.setYinshuacostname("印刷");induscost.setYinshuacost(cost.getYinshuacost());}
					if(cost.getZhizuogongyicost()!=null)
					{total+=cost.getZhizuogongyicost(); induscost.setZhizuogongyicostname("制作工艺成本");induscost.setZhizuogongyicost(cost.getZhizuogongyicost());}
					alltotal+=total;
					k=1;
				}
			}
			if(k==0)
			{
				list=bao.getCostnameandCostmoney(orderlist.get(i).getBackPicId());
				for(int p=0;p<list.size();p++){
					String str=list.get(p).getLabelcostname();
					Double dou=list.get(p).getLabelcostmoney();
					total+=dou;
					alltotal+=dou;
					if(!str.equals("")&&str!=null){
						if(str.equals("防盗锁"))
						{ induscost.setFangdaosuocostname(str);induscost.setFangdaosuocost(dou);}
						if(str.equals("防震垫配件"))
						{induscost.setFangzhengdiancostname(str);induscost.setFangzhengdiancost(dou);}
						if(str.equals("加工成本"))
						{induscost.setJiagongcostname(str);induscost.setJiagongcost(dou);}
						if(str.equals("激光费用"))
						{induscost.setJiguangcostname(str);induscost.setJiguangcost(dou);}
						if(str.equals("烤漆费"))
						{induscost.setKaoqicostname(str);induscost.setKaoqicost(dou);}
						if(str.equals("螺丝配件"))
						{induscost.setLuosicostname(str);induscost.setLuosicost(dou);}
					    if(str.equals("印刷"))
					    {induscost.setYinshuacostname(str);induscost.setYinshuacost(dou);}
						if(str.equals("制作工艺成本"))
						{induscost.setZhizuogongyicostname(str);induscost.setZhizuogongyicost(dou);}
						}
				}
			}
			induscost.setTotal(total);
			total=0.0;
			//induscost.setAlltotal(alltotal);
			induscostlist.add(induscost);
		}

			return SUCCESS;
     }
	public BackstateSer getBao() {
		return bao;
	}
	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}

	public List<Industrycost> getInduscostlist() {
		return induscostlist;
	}
	public void setInduscostlist(List<Industrycost> induscostlist) {
		this.induscostlist = induscostlist;
	}
	public Double getAlltotal() {
		return alltotal;
	}
	public void setAlltotal(Double alltotal) {
		this.alltotal = alltotal;
	}
	public String getIndusname() {
		return indusname;
	}
	public void setIndusname(String indusname) {
		this.indusname = indusname;
	}
	public String getTime1() {
		return time1;
	}
	public void setTime1(String time1) {
		this.time1 = time1;
	}
	public String getTime2() {
		return time2;
	}
	public void setTime2(String time2) {
		this.time2 = time2;
	}
	public String getOrderid() {
		return orderid;
	}
	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}
	public String getOrdername() {
		return ordername;
	}
	public void setOrdername(String ordername) {
		this.ordername = ordername;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public int getPagenow() {
		return pagenow;
	}
	public void setPagenow(int pagenow) {
		this.pagenow = pagenow;
	}
	public int getPagecount() {
		return pagecount;
	}
	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}



}
